package com.anolesoft.qxgl.service.giveRole.impl;

import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.qxgl.dto.giveRole.GiveRoleDto;
import com.anolesoft.qxgl.service.giveRole.inf.GiveRoleServiceInf;
import com.anolesoft.qxgl.service.giveRole.mapper.GiveRoleServiceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GiveRoleServiceInfImpl implements GiveRoleServiceInf {
    @Autowired
    private GiveRoleServiceMapper giveRoleServiceMapper;

    /**
     * 查询当前人员拥有角色
     *
     * @param id
     * @return
     */
    @Override
    public List<String> selectOneGiveRole(String id) {
        return giveRoleServiceMapper.selectRole(id);
    }

    /**
     * 修改角色授权
     *
     * @param giveRoleDto
     * @return
     */
    @Override
    @Transactional
    public int updateAuthorization(GiveRoleDto giveRoleDto) {
        //被修改人主键
        String id = giveRoleDto.getUserUid();
        //被修改人的角色
        List<String> roleUid = giveRoleDto.getRoleUid();
        //获取当前登录人
        LoginUser loginUser = SecurityUtils.getLoginUser();
        giveRoleDto.setCreateUser(loginUser.getUserUid());
        //先删除原角色,再新增角色
        int deleteOldRole = giveRoleServiceMapper.deleteOldRole(id);
        //遍历角色
        for (String str : roleUid) {
            giveRoleDto.setOneRoleUid(str);
            int addNewRole = giveRoleServiceMapper.addNewRole(giveRoleDto);
        }
        return 0;
    }
}
